import request from "@/api/api"
import { reactive, ref } from 'vue'

export const setupdemoadd =(reastus: any)=>{
  const reastu=reactive({
      stu:{
        id:4,
        name:"",
        age:NaN
      }
    })
  
   
    const addstu = ()=>{
      const stu = Object.assign({},reastu.stu);
      reastus.stu.push(stu);
      reastu.stu.id++;
      reastu.stu.name=""
      reastu.stu.age=NaN

    }
     return {
      reastu,
      addstu
    }
}
export const setupdemoinit =()=>{
  
  const reastus = reactive({
      stu : [{
        id:1,
        name:"张三",
        age:10

      },{
        id:2,
        name:"李四",
        age:20

      },{
        id:3,
        name:"王二",
        age:30

      }]
    })
    const remstu = (index: number)=>{
       reastus.stu =reastus.stu.filter((v,i)=>index!=i)

    }
  
     return {
      reastus,
      remstu
    }
}


export const setupParent = ()=>{
    const val = ref("")
    const onClick = (v: string) => {
      console.log('父打印');
      val.value = v
    }
    return {
      onClick,
      val
    }
}

export const setupSon = (props: any, context: any)=>{
    const val = ref("");
    console.log('props:', {
      ...props,
    })
    console.log('context:', {
      ...context,
    })
    console.log('context.attrs:', {
      ...context.attrs,
    })
    const click = ()=>{
      console.log("子打印")
      context.emit("ck",val.value)
    }
   
    return {
      click,
      val
      
    }
   
}
export const getdata = (url: string)=>{
  const obj = {data:{}};
  const mydata = reactive(obj);
   
  
     const getdata = ()=>{
     
      request.post(url, {}).then((res)=>{
          mydata.data = res;
       
      });
      
    }
    getdata();
    return {mydata}
}